package base.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {

	public StringUtil() {
		super();
	}

	/**
	 * 获取异常的详细信息
	 * 
	 * @param e
	 *            异常
	 * @return 异常的详细信息
	 */
	public static String getExceptionDetail(Exception e) {
		StringBuffer sb = new StringBuffer();
		if (null != e) {
			StackTraceElement[] ste = e.getStackTrace();
			sb.append(e.getMessage() + "\n");
			for (int i = 0; i < ste.length; i++) {
				sb.append(ste[i].toString() + "\n");
			}
		}
		return sb.toString();
	}

	/**
	 * 把密码加密
	 * 
	 * @param password
	 *            输入的密码
	 * @return
	 */
	public static String passwordToMD5(String password) {
		StringBuffer buf = new StringBuffer("");
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(password.getBytes());
			byte b[] = md.digest();
			int i;
			for (int offset = 0; offset < b.length; offset++) {
				i = b[offset];
				if (i < 0) {
					i += 256;
				}
				if (i < 16) {
					buf.append("0");
				}
				buf.append(Integer.toHexString(i));
			}
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return (buf.toString());
	}

	/**
	 * 获得不带时分秒的系统当前时间
	 * 
	 * @return
	 */
	public static Long systemDate() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		return Long.parseLong(sdf.format(new Date()));
	}

	/**
	 * 获得系统带时分秒的系统当前时间
	 * 
	 * @return
	 */
	public static Long systemTime() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		return Long.parseLong(sdf.format(new Date()));
	}

	/**
	 * 获得当前月的第一天
	 * 
	 * @return
	 */
	public static Long firstDay() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		Calendar c = Calendar.getInstance(Locale.CANADA);
		c.set(Calendar.DAY_OF_MONTH, c.getMinimum(Calendar.DAY_OF_MONTH));
		return Long.parseLong(sdf.format(c.getTime()));
	}

	/**
	 * 获得当前年的一月一号
	 * 
	 * @return
	 */
	public static Long yearFirstDay() {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		Calendar c = Calendar.getInstance(Locale.CANADA);
		c.set(Calendar.DAY_OF_YEAR, c.getMinimum(Calendar.DAY_OF_YEAR));
		return Long.parseLong(sdf.format(c.getTime()));
	}

	/**
	 * 格式化时间，只返回日期
	 * 
	 * @param date 格式必须为 yyyy-MM-dd
	 * @return将String类型转换为Date类型
	 * */
	public static Date formatDate(String date) {
		Date newDate = null;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			newDate = sdf.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return newDate;
	}

	/**
	 * 获取现在时间
	 * 
	 * @return返回字符串格式 yyyy/MM/dd HH:mm:ss
	 */
	public static String getStringDate() {
		Date currentTime = new Date();
		String dateString = null;
		try {
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
			dateString = formatter.format(currentTime);
		}catch (Exception e) {
			e.printStackTrace();
		}
		return dateString;
	}
	
	/**
	 * 获取现在时间
	 * 
	 * @param format 日期格式类型，例如 yyyy/MM/dd HH:mm:ss
	 * @return返回字符串格式
	 */
	public static String getStringDate(String format) {
		Date currentTime = new Date();
		String dateString = null;
		try {
			SimpleDateFormat formatter = new SimpleDateFormat(format);
			dateString = formatter.format(currentTime);
		}catch (Exception e) {
			e.printStackTrace();
		}
		return dateString;
	}

	/**
	 * 获取文件的扩展名
	 * 
	 * @param fileName
	 *            文件的名称
	 * @return
	 */
 	public static String getExtension(String fileName) {
		return fileName.lastIndexOf(".") == -1 ? "" : fileName
				.substring(fileName.lastIndexOf(".") + 1);
	}

	/**
	 * 转换成三位小数点的浮点数
	 * 
	 * @param num
	 *            被计算的数
	 * @return
	 */
	public static float intToFloat(float num) {
		// 格式化小数
		DecimalFormat df = new DecimalFormat("0.000");
		return Float.parseFloat(df.format(num));
	}

	/**
	 * 电话
	 */
	public static Boolean checkPhone(String phone) {
		Pattern p = Pattern.compile("^[1][3-8]+\\d{9}");
		if (null == phone) {
			return false;
		} else {
			Matcher m = p.matcher(phone);
			Boolean b = m.matches();
			return b;
		}
	}

	/**
	 * UUID
	 */
	public static String createUUID() {
		return UUID.randomUUID().toString().replaceAll("-", "");
	}

	public static void main(String[] args) {
		System.out.println(createUUID());
	}
	/**
	 *生成长度为length的随机数 
	 * 
	 */
	public static String getRandomString(int length) { 
	    StringBuffer buffer = new StringBuffer("0123456789abcdefghijklmnopqrstuvwxyz"); 
	    StringBuffer sb = new StringBuffer(); 
	    Random r = new Random(); 
	    int range = buffer.length(); 
	    for (int i = 0; i < length; i ++) { 
	        sb.append(buffer.charAt(r.nextInt(range))); 
	    } 
	    return sb.toString(); 
	}
}
